<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 24.11.13
 * Time: 13:22
 */
defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Products extends Controller_Admin_Admin {
  public function action_index()
  {
    $this->template->title = 'Админ-Продукты';
    $productTable = ORM::factory('Product');
    $products = $productTable->find_all();
    $this->template->content = View::factory('admin/products/index', array('products' => $products));
  }

  public function action_edit()
  {
    $this->template->title = 'Админ-Проукты';
    $product_id = $this->request->param('id');
    $product = ORM::factory('Product', $product_id);
    if ($post = $this->request->post()) {
      $product->title = $post['title'];
      $product->description = $post['description'];
      $product->price = $post['price'];
      $product->category_id = isset($post['child_category_id']) ? $post['child_category_id']: $post['category_id'];
      unset($post['title']);
      unset($post['description']);
      unset($post['price']);
      unset($post['category_id']);
      unset($post['child_category_id']);
      $params = array();
      $i = 0;
      $temp = "";
      foreach ($post as $value) {
        if ($i % 2 == 0) {
          $temp = $value;
        } else {
          $params[$temp] = $value;
        }
        $i ++;
      }
      unset($post);
      $product->params = json_encode($params);
      $product->save();
      if (isset($_FILES['photo'])) {
        $this->_save_image($_FILES['photo'], $product);
      }
      Controller::redirect('admin/products');
    }
    $cat = new Model_Category();
    $cats = $cat->where('parent_id', '=', 0)->find_all();
    $categories = array();
    foreach ($cats as $c) {
      $categories[$c->id] = $c->title;
    }
    $category = ORM::factory('Category', $product->category_id);
    $parent = ORM::factory('Category', $category->parent_id);

    $children = $cat->where('parent_id', '=', $parent)->find_all();
    $children_categories = array();
    $children_categories[0] = '';
    foreach ($children as $c) {
      $children_categories[$c->id] = $c->title;
    }

    $this->template->content = View::factory('admin/products/edit', array('product' => $product, 'categories' => $categories, 'children' => $children_categories));
  }

  public function action_delete()
  {
    $this->template->title = 'Админ-Продукты';
    $product_id = $this->request->param('id');
    $product = ORM::factory('Product', $product_id);
    if ($post = $this->request->post()) {
      $product->delete();
      Controller::redirect('admin/products');
    }
    $this->template->content = View::factory('admin/products/delete', array('product' => $product));
  }

  public function action_create()
  {
    $this->template->title = 'Админ-Продукты';

    if ($post = $this->request->post()) {
      $product = new Model_Product();
      $product->title = $post['title'];
      $product->description = $post['description'];
      $product->price = $post['price'];
      $product->category_id = isset($post['child_category_id']) ? $post['child_category_id']: $post['category_id'];;
      unset($post['title']);
      unset($post['description']);
      unset($post['price']);
      unset($post['category_id']);
      unset($post['child_category_id']);
      $params = array();
      $i = 0;
      $temp = "";
      foreach ($post as $value) {
        if ($i % 2 == 0) {
          $temp = $value;
        } else {
          $params[$temp] = $value;
        }
        $i ++;
      }
      unset($post);
      $product->params = json_encode($params);
      $product->save();
      if (isset($_FILES['photo'])) {
        $this->_save_image($_FILES['photo'], $product);
      }
      Controller::redirect('admin/products');
    }
    $cat = new Model_Category();
    $cats = $cat->where('parent_id', '=', 0)->find_all();
    $categories = array();
    $categories[0] = '';
    foreach ($cats as $c) {
      $categories[$c->id] = $c->title;
    }
    $this->template->content = View::factory('admin/products/create', array('categories' => $categories));
  }

  protected function _save_image($image, $product)
  {
    if (
      ! Upload::valid($image) or
      ! Upload::not_empty($image) or
      ! Upload::type($image, array('jpg', 'jpeg', 'png', 'gif')))
    {
      return FALSE;
    }

    $directory = DOCROOT . 'uploads/' . $product->id . '/';
    @mkdir($directory);
    if ($file = Upload::save($image, NULL, $directory))
    {
      @unlink($directory.'main.jpg');
      Image::factory($file)
        ->save($directory.'main.jpg');

      @unlink($directory.'profile.jpg');
      Image::factory($file)
        ->resize(200, NULL)
        ->save($directory.'profile.jpg');

      @unlink($directory.'normal.jpg');
      Image::factory($file)
        ->resize(300, NULL)
        ->save($directory.'normal.jpg');

      // Delete the temporary file
      unlink($file);

      return true;
    }

    return FALSE;
  }
}

